package com.cdck.safe.dete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdck.safe.dete.model.entity.ActivityUser;
import com.cdck.safe.dete.mapper.ActivityUserMapper;
import com.cdck.safe.dete.model.form.ActivityUserForm;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.service.ActivityService;
import com.cdck.safe.dete.service.ActivityUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ActivityUser)表服务实现类
 *
 * @author wp
 * @since 2024-02-26 22:59:21
 */
@Service
@RequiredArgsConstructor
public class ActivityUserServiceImpl extends ServiceImpl<ActivityUserMapper, ActivityUser> implements ActivityUserService {

    @Autowired
    private ActivityService activityService;

    /**
     * 通过ID查询单条数据
     *
     * @param activityId 活动id
     * @param userId 用户id
     * @return 实例对象
     */
    @Override
    public ActivityUser queryIsParticipate(Long activityId, Long userId) {
        LambdaQueryWrapper<ActivityUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ActivityUser::getActivityId, activityId)
                .eq(ActivityUser::getUserId, userId);
        return this.getOne(wrapper);
    }

    /**
     * 分页查询
     *
     * queryParams 筛选条件
     * @return 查询结果
     */
    @Override
    public IPage<ActivityUser> queryByPage(ActivityUserForm queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        Page<ActivityUser> page = new Page<>(pageNum, pageSize);
        return this.page(
                page,
                new LambdaQueryWrapper<ActivityUser>()
                        .eq(ActivityUser::getActivityUserId,queryParams.getActivityId())
        );
     }

    /**
     * 新增数据
     *
     * @param activityUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean saveActivityUser(ActivityUser activityUser) {
        activityService.addMember(activityUser.getActivityId());
       return this.save(activityUser);
    }

    /**
     * 修改数据
     *
     * @param activityUser 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateActivityUser(Long id, ActivityUser activityUser) {
         return this.updateById(activityUser);
    }

    /**
     * 通过主键删除数据
     *
     * @param idsStr ids集合
     * @return 是否成功
     */

    @Override
    public boolean deleteActivityUsers(String idsStr) {
         Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        activityService.removeMembers(ids);
        return this.removeByIds(ids);
    }
}
